package com.dartfar.workflow.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dartfar.base.action.BaseAction;
import com.dartfar.base.vo.DataGridVo;
import com.dartfar.workflow.model.WorkflowProcessFunMount;
import com.dartfar.workflow.service.WorkflowProcessFunMountService;
import com.dartfar.workflow.vo.WorkflowProcessFunMountVo;

/**
 * 流程挂接Action
 * @author 王琦
 *
 */
@Scope("prototype")
@Component("workflowProcessFunMountAction")
public class WorkflowProcessFunMountAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	static Logger logger = Logger.getLogger(WorkflowProcessFunMountAction.class);
	
	@Resource
	private WorkflowProcessFunMountService workflowProcessFunMountService;
	
	private int id;
	private int wpfmId;
	
	/**
	 * 页面跳转
	 * @return pl_List
	 */
	public String show(){
		
		return "show";
	}
	
	/**
	 * 启动流程
	 */
	public void doStratProcess(){
		try{
			logger.info("wpfmId: " + wpfmId);
			logger.info("id: " + id);
			JSONObject returnObj = new JSONObject();
			workflowProcessFunMountService.startProcess(curPerson(), wpfmId, id, returnObj);
			
			//String jsonResult = JSONObject.fromObject(workflowProcessFunMountDataGridVo).toString();
			logger.info("jsonResult: " + returnObj);
			try{
				response.getWriter().write(returnObj.toString());
			}catch(Exception e){
				e.printStackTrace();
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 挂接流程
	 */
	public String mountWorkflowProcessFun(){
		WorkflowProcessFunMount workflowProcessFunMount = workflowProcessFunMountService.get(id);
		workflowProcessFunMountService.mountProcess(workflowProcessFunMount);
		return null;
	}
	
	/**
	 * 取消挂接
	 */
	public String unMountWorkflowProcessFun(){
		WorkflowProcessFunMount workflowProcessFunMount = workflowProcessFunMountService.get(id);
		workflowProcessFunMountService.cancelMountProcess(workflowProcessFunMount);
		return null;
	}
	
	/**
	 * 加载工作流流程功能挂接数据
	 * @return JSON
	 */
	public String loadWorkflowProcessFunMountData(){
		
		DataGridVo<WorkflowProcessFunMountVo> workflowProcessFunMountDataGridVo = new DataGridVo<WorkflowProcessFunMountVo>();
		List<WorkflowProcessFunMountVo> wpfmVos = new ArrayList<WorkflowProcessFunMountVo>();
		try{
		
		List<WorkflowProcessFunMount> list = workflowProcessFunMountService.getListForPage(pageNo, pageSize);
		
		for(Iterator<WorkflowProcessFunMount> w =  list.iterator(); w.hasNext();){
			WorkflowProcessFunMount wpfm = w.next();
			WorkflowProcessFunMountVo wpfmVo = new WorkflowProcessFunMountVo();
			wpfmVo.setId(wpfm.getId());
			wpfmVo.setDeployId(wpfm.getDeployId());
			wpfmVo.setProcessName(wpfm.getProcessName());
			wpfmVo.setProcessKey(wpfm.getProcessKey());
			wpfmVo.setModelName(wpfm.getModelName());
			wpfmVo.setModelIdName(wpfm.getModelIdName());
			wpfmVo.setMountStatus(wpfm.getMountStatus());
			wpfmVo.setVersion(wpfm.getVersion().toString());
			wpfmVos.add(wpfmVo);
		}
		
		logger.info("list: " + list.size());

		Long count = workflowProcessFunMountService.getCount();
		
		workflowProcessFunMountDataGridVo.setTotal(count);
		workflowProcessFunMountDataGridVo.setRows(wpfmVos);
		
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		String jsonResult = JSONObject.fromObject(workflowProcessFunMountDataGridVo).toString();
		logger.info("jsonResult: " + jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Getter And Setter
	 */
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getWpfmId() {
		return wpfmId;
	}

	public void setWpfmId(int wpfmId) {
		this.wpfmId = wpfmId;
	}
	

	

}
